$(function(){


	allmaney();
	function allmaney(){
			var price = 0;
	 	$('td.p_price em').each(function(index,obj){
		 		price  += (parseFloat($(obj).html())*parseInt($('.only_num').eq(index).val()))*100;
		 })
		 $('.shop_num span').html($('td.p_price em').length)
		 var all  = price
		 $('.total_maney em,.end_maney em').html(toZero(all/100));
		}
	 var time = null;
	//点击加的数量的时候
	$('.dec').click(function(){
		$('.calcuate').not($(this)).hide();
		clearTimeout(time);
		var index = $('.dec').index(this);
	   var data = $(this).attr('data');
		var par = $(this).parent()
		var num = par.children('.only_num').val();
		var  all = parseFloat($('.total_maney em').html())*100;
		num++;
		par.children('.only_num').val(num);
		par.children('.calcuate').show()
		time = setTimeout(function(){
			par.children('.calcuate').hide()
		},2000);
		var money = parseFloat(data*100)*parseInt(num);
		money = toZero(money/100);
		par.children('.calcuate').children('.sum').children('span').html("￥"+money);
		all += parseFloat(data*100)*parseInt(1);
		$('.total_maney em,.end_maney em').html(toZero(all/100));
	})
	//点击减少的时候
		$('.add').click(function(){
			clearTimeout(time);
		$('.calcuate').not($(this)).hide()
		var index = $('.add').index(this);
	    var data = $(this).attr('data');
		var par = $(this).parent()
		var num = par.children('.only_num').val();
		var  all = parseFloat($('.total_maney em').html())*100;
		num--;
		if(num>=1){
			par.children('.only_num').val(num);
			par.children('.calcuate').show()
			time =setTimeout(function(){
				par.children('.calcuate').hide()
			},2000);
			var money = parseFloat(data*100)*parseInt(num);
			money = toZero(money/100);
			par.children('.calcuate').children('.sum').children('span').html("￥"+money);
			all -= parseFloat(data*100)*parseInt(1);
			$('.total_maney em,.end_maney em').html(toZero(all/100));
		}else{
			delX($(this),index)

		}
	})
	//删除提示
	$('.d_close,.cancel').click(function(){
		$('.delete').hide()
	})
	//点击确定删除
	$('.sure').live('click',function(){
		var check = $(this).parent().parent().attr('check');
		if(check==0){
		var index = $(this).parent().parent().attr('num');
		$('.shop_list').eq(index).remove();

		}else{
			$('.checkbox:checked').parent().parent().parent().remove()
		}
		$('.delete').hide();
		allmaney();
		isShop();

	})
	//点击取消
	$('.close').click(function(){
		 $(this).parent().hide();
	})
	//点击删除
	$('.delete_btn').live('click',function(){
		var index = $('.delete_btn').index(this);
		delX($(this),index)
	})
    //判断购物车里是否有商品
    function isShop(){
    	if($('.shop_list').length==0){
    		$('.shop_car,.car_hd p,.car_btn').hide();
    		$('.no_com').show();
    	}
    }
	/***
	*获取删除位置
	**/
	function delX(obj,index){
			var x = parseInt(obj.offset().left)-parseInt($('.shop_car').offset().left)-$('.delete').width()-5;
			var y = parseInt(obj.offset().top)- parseInt($('.shop_car').offset().top);
			$('.delete').css({left:x,top:y})
			$('.delete').show();
			$('.delete').attr('num',index);
			$('.delete').attr('check',0);
	}


	//全选
	$('.allbox').toggle(function(){
		$(this).attr('checked',true)
		$('.checkbox').attr('checked',true)
	},function(){
		$(this).attr('checked',false)
		$('.checkbox').attr('checked',false)
	})

	//删除选中
	$('.del_check').click(function(){
		var x = 20;
		var y = parseInt($(this).offset().top)- parseInt($('.shop_car').offset().top)+20;
		$('.delete').css({left:x,top:y})
	    $('.delete').show();
	    $('.delete').attr('check',1);

	})

	//补零
	function toZero (obj){
		var s = obj.toString();
		var i = s.indexOf('.');
		if(i<0){
			i = s.length;
			s +='.';
		}
		while( s.length<=i+2){
			s +='0';
		}
		return s;
	}
})